/*
 * GccApplication2.c
 *
 * Created: 4/21/2015 10:28:53 AM
 *  Author: schussle
 */ 

#include <avr/io.h>
#include <util/delay.h>

// my_delay takes an integer argument and uses the _delay_ms function to delay for that amount of milliseconds
void my_delay(unsigned int ms)
{
	while(ms > 0)        // loop until finished with delay
	{
		_delay_ms(1);    // delay 1ms
		ms--;            // decrement total requested delay
	}
}

int main(void)
{
	int delay;        // value to store delay
	ADCSRA = 0x87;    // ADC enable with select prescaler of 128
	ADMUX = 0xC0;    // 1.1 V reference and ADC0 left justified
	
	DDRB = 0xFF;    // PB as output for ULN2003a
	DDRC = 0;        // enable PC0 for analog input
	
	while(1)
	{
		ADCSRA|=(1<<ADSC);                    // start conversion
		while ((ADCSRA&(1<<ADIF))==0);        // wait for conversion to finish
		delay = ADCL + (ADCH * 256);        // combine converted number into single integer (delay)
		if (delay < 75)                        // if delay is less than 75, set to default of 75
		delay = 75;                        // set delay to 75
		
		PORTB = 0X66;            // turning stepper clockwise
		my_delay(delay);        // delay for value read from potentiometer
		PORTB = 0xCC;            // turning stepper clockwise
		my_delay(delay);        // delay for value read from potentiometer
		PORTB = 0x99;            // turning stepper clockwise
		my_delay(delay);        // delay for value read from potentiometer
		PORTB = 0x33;            // turning stepper clockwise
		my_delay(delay);        // delay for value read from potentiometer
	}
	
	return 0;
}